#!/usr/bin/perl
# Version 0.2.1

$testcase = "mismatch";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 5) {
        print("Usage: $testcase.pl dump_file1 .. dump_file5 log_file");
        exit 1;
}

$dumpfile[0] = $ARGV[0];
$dumpfile[1] = $ARGV[1];
$dumpfile[2] = $ARGV[2];
$dumpfile[3] = $ARGV[3];
$dumpfile[4] = $ARGV[4];
$logfile = $ARGV[5];

#------------------------------------------------------------------------------

@local_verdict = ( 3, 3, 3, 3, 3);

# To check if there's an adjacency forming between two neighbors the existence of
# non-hello packets is enough for us. 
$success = 1;
open(FILE, $dumpfile[0]) or $success = 0;

if($success eq 1) {
	$local_verdict[0] = 1;
		
	while(<FILE>) {
				
		if($_ =~ /OSPFv2-hello/) {
			$dead = 40;
			if($_ =~ /dead (\d*)/) {
				$dead = $1;
			}
					
		# look only at OSPF hello packets
			if( $_ !~ /nbrs/ ) {
				@al = split(/ /, $_);
				
				# now look at the time
				@ti = split(/\./, $al[0]);
				@ts = split(/:/, $ti[0]);
		
				$secs = $ts[2] + ($ts[1] + $ts[0] * 60) * 60;
				
				if($secs > $dead) {
						
					$local_verdict[0] = 2;
				}
			}
		}
	}
		
	close(FILE);
}


for( $i = 1; $i < 4; $i++) {
	# Walk through the remaining logfiles. It mustn't contain other packets than hellos
	# and none of them should contain a neighbor list.
	$success = 1;
	
	open(FILE, $dumpfile[$i]) or $success = 0;
	
	if($success eq 1) {
		
		$local_verdict[$i] = 1;
		
		while(<FILE>) {
			if($_ =~ /OSPFv2/) {
				# look only at OSPF packets
				if( $_ =~ /hello/ ) {
					if( $_ =~ /nbrs/ ) {
						# hello packet contains neighbors
						$local_verdict[$i] = 2;
					}
				} else {
					# not a hello packet
					$local_verdict[$i] = 2;
				}
			} 
		}
		
		close(FILE);
	}
}

$success = 1;
	
open(FILE, $dumpfile[4]) or $success = 0;
	
if($success eq 1) {
		
	$local_verdict[4] = 2;
		
	while(<FILE>) {
		if($_ =~ /0\.0\.0\.1 > (.*) OSPFv2-hello/) {
			# look only at OSPF packets from 0.0.0.1
			if( $_ =~ /nbrs 0\.0\.0\.2/ ) {
				# neighbor must be detected, because netmask is not checked on ptp nets
				$local_verdict[4] = 1;
			}
		} 
	}
		
	close(FILE);
}

for( $i = 0; $i < 5; $i++) {	
	if ($local_verdict[$i] eq 1) {
		print("Evaluation of $dumpfile[$i] done. Test PASSED.\n");		
		
		if($i eq 0) {
			$verdict = 1;
		}
		
	} elsif ($local_verdict[$i] eq 2) {
		print("Evaluation of $dumpfile[$i] done. Errors were found. Test FAILED.\n");
		
		$verdict = 2;
	} elsif ($local_verdict[$i] eq 3) {
		print("$dumpfile[$i] cannot be evaluated. Test INCONCLUSIVE.\n");
		
		if($verdict ne 2) {
			$verdict = 3;
		}
	}
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");